<!doctype html>
<html class="default no-js">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>&quot;src/func/event-loop&quot; | @m-fe/utils</title>
	<meta name="description" content="Documentation for @m-fe/utils">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="../assets/css/main.css">
</head>
<body>
<header>
	<div class="tsd-page-toolbar">
		<div class="container">
			<div class="table-wrap">
				<div class="table-cell" id="tsd-search" data-index="../assets/js/search.json" data-base="..">
					<div class="field">
						<label for="tsd-search-field" class="tsd-widget search no-caption">Search</label>
						<input id="tsd-search-field" type="text" />
					</div>
					<ul class="results">
						<li class="state loading">Preparing search index...</li>
						<li class="state failure">The search index is not available</li>
					</ul>
					<a href="../index.html" class="title">@m-fe/utils</a>
				</div>
				<div class="table-cell" id="tsd-widgets">
					<div id="tsd-filter">
						<a href="#" class="tsd-widget options no-caption" data-toggle="options">Options</a>
						<div class="tsd-filter-group">
							<div class="tsd-select" id="tsd-filter-visibility">
								<span class="tsd-select-label">All</span>
								<ul class="tsd-select-list">
									<li data-value="public">Public</li>
									<li data-value="protected">Public/Protected</li>
									<li data-value="private" class="selected">All</li>
								</ul>
							</div>
							<input type="checkbox" id="tsd-filter-inherited" checked />
							<label class="tsd-widget" for="tsd-filter-inherited">Inherited</label>
							<input type="checkbox" id="tsd-filter-externals" checked />
							<label class="tsd-widget" for="tsd-filter-externals">Externals</label>
						</div>
					</div>
					<a href="#" class="tsd-widget menu no-caption" data-toggle="menu">Menu</a>
				</div>
			</div>
		</div>
	</div>
	<div class="tsd-page-title">
		<div class="container">
			<ul class="tsd-breadcrumb">
				<li>
					<a href="../globals.html">Globals</a>
				</li>
				<li>
					<a href="_src_func_event_loop_.html">&quot;src/func/event-loop&quot;</a>
				</li>
			</ul>
			<h1>Module &quot;src/func/event-loop&quot;</h1>
		</div>
	</div>
</header>
<div class="container container-main">
	<div class="row">
		<div class="col-8 col-content">
			<section class="tsd-panel-group tsd-index-group">
				<h2>Index</h2>
				<section class="tsd-panel tsd-index-panel">
					<div class="tsd-index-content">
						<section class="tsd-index-section ">
							<h3>Interfaces</h3>
							<ul class="tsd-index-list">
								<li class="tsd-kind-interface tsd-parent-kind-module"><a href="../interfaces/_src_func_event_loop_.eventlooptask.html" class="tsd-kind-icon">Event<wbr>Loop<wbr>Task</a></li>
							</ul>
						</section>
						<section class="tsd-index-section ">
							<h3>Variables</h3>
							<ul class="tsd-index-list">
								<li class="tsd-kind-variable tsd-parent-kind-module"><a href="_src_func_event_loop_.html#defer" class="tsd-kind-icon">defer</a></li>
								<li class="tsd-kind-variable tsd-parent-kind-module"><a href="_src_func_event_loop_.html#nexttick" class="tsd-kind-icon">next<wbr>Tick</a></li>
								<li class="tsd-kind-variable tsd-parent-kind-module"><a href="_src_func_event_loop_.html#raf" class="tsd-kind-icon">raf</a></li>
							</ul>
						</section>
						<section class="tsd-index-section ">
							<h3>Functions</h3>
							<ul class="tsd-index-list">
								<li class="tsd-kind-function tsd-parent-kind-module"><a href="_src_func_event_loop_.html#clearloops" class="tsd-kind-icon">clear<wbr>Loops</a></li>
								<li class="tsd-kind-function tsd-parent-kind-module"><a href="_src_func_event_loop_.html#getraf" class="tsd-kind-icon">get<wbr>Raf</a></li>
								<li class="tsd-kind-function tsd-parent-kind-module"><a href="_src_func_event_loop_.html#loop" class="tsd-kind-icon">loop</a></li>
								<li class="tsd-kind-function tsd-parent-kind-module"><a href="_src_func_event_loop_.html#setraf" class="tsd-kind-icon">set<wbr>Raf</a></li>
							</ul>
						</section>
					</div>
				</section>
			</section>
			<section class="tsd-panel-group tsd-member-group ">
				<h2>Variables</h2>
				<section class="tsd-panel tsd-member tsd-kind-variable tsd-parent-kind-module">
					<a name="defer" class="tsd-anchor"></a>
					<h3><span class="tsd-flag ts-flagConst">Const</span> defer</h3>
					<div class="tsd-signature tsd-kind-icon">defer<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">setTimeout</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">then</span><span class="tsd-signature-symbol"> = typeof Promise &#x3D;&#x3D;&#x3D; &#x27;function&#x27;? Promise.resolve().then.bind(Promise.resolve()): setTimeout</span></div>
					<aside class="tsd-sources">
					</aside>
					<div class="tsd-comment tsd-typography">
						<div class="lead">
							<p>异步调用某个函数的简单实现，这里混用了 MicroTask 与 MacroTask</p>
						</div>
					</div>
				</section>
				<section class="tsd-panel tsd-member tsd-kind-variable tsd-parent-kind-module">
					<a name="nexttick" class="tsd-anchor"></a>
					<h3><span class="tsd-flag ts-flagConst">Const</span> next<wbr>Tick</h3>
					<div class="tsd-signature tsd-kind-icon">next<wbr>Tick<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">queueNextTick</span><span class="tsd-signature-symbol"> = (() &#x3D;&gt; {// 需要执行的回调列表const callbacks: Function[] &#x3D; [];// 是否处于挂起状态let pending &#x3D; false;// 时间函数句柄let timerFunc: Function;// 执行并且清空所有的回调列表function nextTickHandler() {pending &#x3D; false;const copies &#x3D; callbacks.slice(0);callbacks.length &#x3D; 0;// eslint-disable-next-line @typescript-eslint/prefer-for-offor (let i &#x3D; 0; i &lt; copies.length; i++) {copies[i]();}}// nextTick 的回调会被加入到 MicroTask 队列中，这里我们主要通过原生的 Promise 与 MutationObserver 实现/* istanbul ignore if */if (typeof Promise !&#x3D;&#x3D; &#x27;undefined&#x27; &amp;&amp; isNative(Promise)) {const p &#x3D; Promise.resolve();const logError &#x3D; (err: Error) &#x3D;&gt; {console.error(err);};timerFunc &#x3D; () &#x3D;&gt; {p.then(nextTickHandler).catch(logError);// 在部分 iOS 系统下的 UIWebViews 中，Promise.then 可能并不会被清空，因此我们需要添加额外操作以触发if (isIOS) setTimeout(noop);};} else if (typeof MutationObserver !&#x3D;&#x3D; &#x27;undefined&#x27; &amp;&amp;(isNative(MutationObserver) ||// PhantomJS and iOS 7.xMutationObserver.toString() &#x3D;&#x3D;&#x3D; &#x27;[object MutationObserverConstructor]&#x27;)) {// 当 Promise 不可用时候使用 MutationObserver// e.g. PhantomJS IE11, iOS7, Android 4.4let counter &#x3D; 1;const observer &#x3D; new MutationObserver(nextTickHandler);const textNode &#x3D; document.createTextNode(String(counter));observer.observe(textNode, {characterData: true,});timerFunc &#x3D; () &#x3D;&gt; {counter &#x3D; (counter + 1) % 2;textNode.data &#x3D; String(counter);};} else {// 如果都不存在，则回退使用 setTimeout/* istanbul ignore next */timerFunc &#x3D; () &#x3D;&gt; {setTimeout(nextTickHandler, 0);};}return function queueNextTick(cb?: Function,ctx?: object,): void | Promise&lt;any&gt; {let _resolve: Function;callbacks.push(() &#x3D;&gt; {if (cb) {try {cb.call(ctx);} catch (e) {handleError(e, ctx, &#x27;nextTick&#x27;);}} else if (_resolve) {_resolve(ctx);}});if (!pending) {pending &#x3D; true;timerFunc();}// 如果没有传入回调，则表示以异步方式调用if (!cb &amp;&amp; typeof Promise !&#x3D;&#x3D; &#x27;undefined&#x27;) {return new Promise((resolve, _reject) &#x3D;&gt; {_resolve &#x3D; resolve;});}};})()</span></div>
					<aside class="tsd-sources">
					</aside>
					<div class="tsd-comment tsd-typography">
						<div class="lead">
							<p>使用 MicroTask 来异步执行批次任务</p>
						</div>
					</div>
				</section>
				<section class="tsd-panel tsd-member tsd-kind-variable tsd-parent-kind-module">
					<a name="raf" class="tsd-anchor"></a>
					<h3><span class="tsd-flag ts-flagLet">Let</span> raf</h3>
					<div class="tsd-signature tsd-kind-icon">raf<span class="tsd-signature-symbol">:</span> <a href="_src_func_utils_.html#noop" class="tsd-signature-type">noop</a><span class="tsd-signature-symbol"> = inBrowser? (cb: FrameRequestCallback) &#x3D;&gt; requestAnimationFrame(cb): noop</span></div>
					<aside class="tsd-sources">
					</aside>
					<div class="tsd-comment tsd-typography">
						<div class="lead">
							<p>兼容不同环境的 requestAnimationFrame 实现</p>
						</div>
					</div>
				</section>
			</section>
			<section class="tsd-panel-group tsd-member-group ">
				<h2>Functions</h2>
				<section class="tsd-panel tsd-member tsd-kind-function tsd-parent-kind-module">
					<a name="clearloops" class="tsd-anchor"></a>
					<h3>clear<wbr>Loops</h3>
					<ul class="tsd-signatures tsd-kind-function tsd-parent-kind-module">
						<li class="tsd-signature tsd-kind-icon">clear<wbr>Loops<span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">void</span></li>
					</ul>
					<ul class="tsd-descriptions">
						<li class="tsd-description">
							<aside class="tsd-sources">
							</aside>
							<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4>
						</li>
					</ul>
				</section>
				<section class="tsd-panel tsd-member tsd-kind-function tsd-parent-kind-module">
					<a name="getraf" class="tsd-anchor"></a>
					<h3>get<wbr>Raf</h3>
					<ul class="tsd-signatures tsd-kind-function tsd-parent-kind-module">
						<li class="tsd-signature tsd-kind-icon">get<wbr>Raf<span class="tsd-signature-symbol">(</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="_src_func_utils_.html#noop" class="tsd-signature-type">noop</a></li>
					</ul>
					<ul class="tsd-descriptions">
						<li class="tsd-description">
							<aside class="tsd-sources">
							</aside>
							<h4 class="tsd-returns-title">Returns <a href="_src_func_utils_.html#noop" class="tsd-signature-type">noop</a></h4>
						</li>
					</ul>
				</section>
				<section class="tsd-panel tsd-member tsd-kind-function tsd-parent-kind-module">
					<a name="loop" class="tsd-anchor"></a>
					<h3>loop</h3>
					<ul class="tsd-signatures tsd-kind-function tsd-parent-kind-module">
						<li class="tsd-signature tsd-kind-icon">loop<span class="tsd-signature-symbol">(</span>fn<span class="tsd-signature-symbol">: </span><span class="tsd-signature-symbol">(</span>num<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol"> =&gt; </span><span class="tsd-signature-type">void</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><a href="../interfaces/_src_func_event_loop_.eventlooptask.html" class="tsd-signature-type">EventLoopTask</a></li>
					</ul>
					<ul class="tsd-descriptions">
						<li class="tsd-description">
							<aside class="tsd-sources">
							</aside>
							<h4 class="tsd-parameters-title">Parameters</h4>
							<ul class="tsd-parameters">
								<li>
									<h5>fn: <span class="tsd-signature-symbol">(</span>num<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol"> =&gt; </span><span class="tsd-signature-type">void</span></h5>
									<ul class="tsd-parameters">
										<li class="tsd-parameter-signature">
											<ul class="tsd-signatures tsd-kind-type-literal">
												<li class="tsd-signature tsd-kind-icon"><span class="tsd-signature-symbol">(</span>num<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">number</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">void</span></li>
											</ul>
											<ul class="tsd-descriptions">
												<li class="tsd-description">
													<h4 class="tsd-parameters-title">Parameters</h4>
													<ul class="tsd-parameters">
														<li>
															<h5>num: <span class="tsd-signature-type">number</span></h5>
														</li>
													</ul>
													<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4>
												</li>
											</ul>
										</li>
									</ul>
								</li>
							</ul>
							<h4 class="tsd-returns-title">Returns <a href="../interfaces/_src_func_event_loop_.eventlooptask.html" class="tsd-signature-type">EventLoopTask</a></h4>
						</li>
					</ul>
				</section>
				<section class="tsd-panel tsd-member tsd-kind-function tsd-parent-kind-module">
					<a name="setraf" class="tsd-anchor"></a>
					<h3>set<wbr>Raf</h3>
					<ul class="tsd-signatures tsd-kind-function tsd-parent-kind-module">
						<li class="tsd-signature tsd-kind-icon">set<wbr>Raf<span class="tsd-signature-symbol">(</span>_raf<span class="tsd-signature-symbol">: </span><span class="tsd-signature-symbol">typeof </span><a href="_src_func_event_loop_.html#raf" class="tsd-signature-type">raf</a><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">void</span></li>
					</ul>
					<ul class="tsd-descriptions">
						<li class="tsd-description">
							<aside class="tsd-sources">
							</aside>
							<h4 class="tsd-parameters-title">Parameters</h4>
							<ul class="tsd-parameters">
								<li>
									<h5>_raf: <span class="tsd-signature-symbol">typeof </span><a href="_src_func_event_loop_.html#raf" class="tsd-signature-type">raf</a></h5>
								</li>
							</ul>
							<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4>
						</li>
					</ul>
				</section>
			</section>
		</div>
		<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
			<nav class="tsd-navigation primary">
				<ul>
					<li class="globals  ">
						<a href="../globals.html"><em>Globals</em></a>
					</li>
					<li class="current tsd-kind-module">
						<a href="_src_func_event_loop_.html">&quot;src/func/event-<wbr>loop&quot;</a>
					</li>
				</ul>
			</nav>
			<nav class="tsd-navigation secondary menu-sticky">
				<ul class="before-current">
					<li class=" tsd-kind-interface tsd-parent-kind-module">
						<a href="../interfaces/_src_func_event_loop_.eventlooptask.html" class="tsd-kind-icon">Event<wbr>Loop<wbr>Task</a>
					</li>
					<li class=" tsd-kind-variable tsd-parent-kind-module">
						<a href="_src_func_event_loop_.html#defer" class="tsd-kind-icon">defer</a>
					</li>
					<li class=" tsd-kind-variable tsd-parent-kind-module">
						<a href="_src_func_event_loop_.html#nexttick" class="tsd-kind-icon">next<wbr>Tick</a>
					</li>
					<li class=" tsd-kind-variable tsd-parent-kind-module">
						<a href="_src_func_event_loop_.html#raf" class="tsd-kind-icon">raf</a>
					</li>
					<li class=" tsd-kind-function tsd-parent-kind-module">
						<a href="_src_func_event_loop_.html#clearloops" class="tsd-kind-icon">clear<wbr>Loops</a>
					</li>
					<li class=" tsd-kind-function tsd-parent-kind-module">
						<a href="_src_func_event_loop_.html#getraf" class="tsd-kind-icon">get<wbr>Raf</a>
					</li>
					<li class=" tsd-kind-function tsd-parent-kind-module">
						<a href="_src_func_event_loop_.html#loop" class="tsd-kind-icon">loop</a>
					</li>
					<li class=" tsd-kind-function tsd-parent-kind-module">
						<a href="_src_func_event_loop_.html#setraf" class="tsd-kind-icon">set<wbr>Raf</a>
					</li>
				</ul>
			</nav>
		</div>
	</div>
</div>
<footer class="with-border-bottom">
	<div class="container">
		<h2>Legend</h2>
		<div class="tsd-legend-group">
			<ul class="tsd-legend">
				<li class="tsd-kind-variable"><span class="tsd-kind-icon">Variable</span></li>
				<li class="tsd-kind-function"><span class="tsd-kind-icon">Function</span></li>
				<li class="tsd-kind-function tsd-has-type-parameter"><span class="tsd-kind-icon">Function with type parameter</span></li>
				<li class="tsd-kind-type-alias"><span class="tsd-kind-icon">Type alias</span></li>
				<li class="tsd-kind-type-alias tsd-has-type-parameter"><span class="tsd-kind-icon">Type alias with type parameter</span></li>
			</ul>
			<ul class="tsd-legend">
				<li class="tsd-kind-class"><span class="tsd-kind-icon">Class</span></li>
				<li class="tsd-kind-class tsd-has-type-parameter"><span class="tsd-kind-icon">Class with type parameter</span></li>
			</ul>
			<ul class="tsd-legend">
				<li class="tsd-kind-interface"><span class="tsd-kind-icon">Interface</span></li>
			</ul>
		</div>
	</div>
</footer>
<div class="container tsd-generator">
	<p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p>
</div>
<div class="overlay"></div>
<script src="../assets/js/main.js"></script>
</body>
</html>